<template>
  <div class="home">
    <header>
      <h1>健康管理系统</h1>
      <nav>
        <router-link to="/login" class="login-link">登录</router-link>
      </nav>
    </header>
    
    <main>
      <!-- 健康资讯部分 -->
      <section class="health-news-section">
        <h2>最新健康资讯</h2>
        <div v-if="loading" class="loading">加载中...</div>
        <div v-else-if="newsList.length === 0" class="no-news">暂无资讯</div>
        <div v-else class="news-list">
          <div 
            v-for="news in newsList" 
            :key="news.id" 
            class="news-item"
          >
            <div class="news-content">
              <!-- 图片 -->
              <div v-if="news.title_image" class="news-image-container">
                <img 
                  :src="news.title_image" 
                  :alt="news.title" 
                  class="news-image"
                  @error="handleImageError"
                />
              </div>
              
              <!-- 标题和信息 -->
              <div class="news-info">
                <h3 class="news-title">
                  <a :href="news.url" target="_blank" rel="noopener noreferrer">
                    {{ news.title }}
                  </a>
                </h3>
                <div class="news-meta">
                  <span class="news-date">{{ formatDate(news.publish_time) }}</span>
                  <!-- <span class="news-source">{{ news.website }}</span> -->
                  <a :href="news.url" target="_blank" rel="noopener noreferrer" class="read-more">
                    阅读原文
                  </a>
                </div>
              </div>
            </div>
          </div>
        </div>
        
        <!-- 分页 -->
        <div class="pagination" v-if="totalPages > 1">
          <el-pagination
            v-model:current-page="currentPage"
            :page-size="pageSize"
            :total="totalItems"
            layout="prev, pager, next, jumper"
            @current-change="handlePageChange"
            background
          />
        </div>
      </section>
    </main>
    
    <footer>
      <p>&copy; 2025 DVHealth. 保留所有权利。</p>
    </footer>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getHealthNews } from '@/api/health'

const newsList = ref([])
const loading = ref(false)
const currentPage = ref(1)
const pageSize = ref(20)
const totalItems = ref(0)
const totalPages = ref(0)

// 获取健康资讯列表
const fetchNews = async (page = 1) => {
  loading.value = true
  try {
    const response = await getHealthNews({
      page: page,
      page_size: pageSize.value
    })
    
    // 处理分页数据
    if (response.results) {
      newsList.value = response.results
      totalItems.value = response.count || 0
      totalPages.value = Math.ceil(totalItems.value / pageSize.value)
    } else {
      newsList.value = response
      totalItems.value = response.length
      totalPages.value = 1
    }
  } catch (error) {
    console.error('获取健康资讯失败:', error)
    newsList.value = []
  } finally {
    loading.value = false
  }
}

// 处理分页变化
const handlePageChange = (page) => {
  // 防御性处理，检查是否为事件对象
  let pageNumber = page
  if (typeof page === 'object' && page.target) {
    // 如果是事件对象，从事件中获取页码
    pageNumber = parseInt(page.target.value) || 1
  }
  
  currentPage.value = pageNumber
  fetchNews(pageNumber)
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '-'
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN')
}

// 处理图片加载错误
const handleImageError = (event) => {
  event.target.style.display = 'none'
}

onMounted(() => {
  fetchNews()
})
</script>

<style scoped>
.home {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 0;
  border-bottom: 1px solid #eee;
}

header h1 {
  margin: 0;
  color: #42b883;
}

nav a {
  margin-left: 20px;
  text-decoration: none;
  color: #42b883;
  font-weight: bold;
  padding: 8px 16px;
  border: 1px solid #42b883;
  border-radius: 4px;
  transition: all 0.3s ease;
}

nav a:hover {
  background-color: #42b883;
  color: white;
}

.hero {
  text-align: center;
  padding: 60px 0;
}

.hero h2 {
  font-size: 2.5em;
  margin-bottom: 20px;
  color: #333;
}

.hero p {
  font-size: 1.2em;
  color: #666;
  max-width: 600px;
  margin: 0 auto;
}

.features {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 30px;
  margin: 60px 0;
}

.feature {
  background: #f9f9f9;
  padding: 30px;
  border-radius: 8px;
  text-align: center;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  transition: transform 0.3s ease;
}

.feature:hover {
  transform: translateY(-5px);
}

.feature h3 {
  color: #42b883;
  margin-bottom: 15px;
}

/* 健康资讯部分样式 */
.health-news-section {
  margin: 60px 0;
}

.health-news-section h2 {
  text-align: center;
  color: #333;
  margin-bottom: 30px;
  font-size: 2em;
  position: relative;
}

.health-news-section h2::after {
  content: '';
  display: block;
  width: 50px;
  height: 3px;
  background: #42b883;
  margin: 10px auto 0;
  border-radius: 2px;
}

.loading, .no-news {
  text-align: center;
  padding: 40px;
  color: #999;
  font-size: 1.1em;
}

.news-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.news-item {
  border: 1px solid #eee;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  background: white;
}

.news-item:hover {
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
}

.news-content {
  display: flex;
  gap: 20px;
}

.news-image-container {
  flex-shrink: 0;
}

.news-image {
  width: 150px;
  height: 100px;
  object-fit: cover;
  border-radius: 6px;
}

.news-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.news-title {
  margin: 0 0 15px 0;
  font-size: 1.2em;
  line-height: 1.4;
}

.news-title a {
  text-decoration: none;
  color: #333;
  font-weight: 600;
}

.news-title a:hover {
  color: #42b883;
  text-decoration: underline;
}

.news-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9em;
  color: #999;
}

.news-date {
  font-weight: bold;
  color: #42b883;
}

.news-source {
  font-weight: bold;
}

.read-more {
  color: #42b883;
  text-decoration: none;
  font-weight: bold;
  padding: 5px 10px;
  border: 1px solid #42b883;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.read-more:hover {
  background-color: #42b883;
  color: white;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 40px;
}

footer {
  text-align: center;
  padding: 30px 0;
  border-top: 1px solid #eee;
  color: #999;
  margin-top: 60px;
}

@media (max-width: 768px) {
  header {
    flex-direction: column;
    gap: 15px;
  }
  
  nav {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
  }
  
  nav a {
    margin: 5px 10px;
  }
  
  .hero h2 {
    font-size: 2em;
  }
  
  .hero p {
    font-size: 1em;
  }
  
  .features {
    grid-template-columns: 1fr;
  }
  
  .news-content {
    flex-direction: column;
  }
  
  .news-image {
    width: 100%;
    height: 200px;
    margin-bottom: 15px;
  }
  
  .news-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }
  
  .health-news-section h2 {
    font-size: 1.5em;
  }
}
</style>